{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 本节课最终成果体验\n",
    "- 下载和查看模板文件template.docx，观察里面有什么特点\n",
    "- 创建名为invite的函数，它的作用是创建邀请函。这个函数和print, input等函数是一样的概念。\n",
    "- 使用invite函数为不同的名字创建邀请函\n",
    "- 示例代码添加统计时间的代码，我们实际运用中不用添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word\n",
    "import time\n",
    "\n",
    "def invite(name):\n",
    "    #1. 指定模板文件的名字\n",
    "    file = 'template.docx'  \n",
    "    #2. 加载模板文件\n",
    "    doc = word.load(file) \n",
    "    #3. 替换标签\n",
    "    word.replace(doc, '<name>', name) \n",
    "    #4. 保存文档\n",
    "    doc.save(name + '.docx') \n",
    "\n",
    "#5. 调用上面写好的函数\n",
    "begin = time.time()\n",
    "invite('张三')\n",
    "invite('李四')\n",
    "end = time.time()\n",
    "print(end-begin)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 自己动手开发自动创建邀请函程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 闯关练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### T31 修改模板-添加标签\n",
    "1. 下载模板文件到本地，并重命名为template2.docx\n",
    "2. 修改下载好的模板文件，在里面多加入一个替换标签&lt;name&gt;\n",
    "3. 上传修改好的模板文件到在线编程系统\n",
    "4. 修改下面的代码，让代码使用新的模板文件\n",
    "5. 运行代码，并查看结果，看看是否新加的标签也被替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "from maioffice import word\n",
    "\n",
    "def invite(name):\n",
    "    file = 'template.docx'\n",
    "    doc = word.load(file)\n",
    "    word.replace(doc, '<name>', name)\n",
    "    doc.save(name + '.docx')\n",
    "\n",
    "invite('张三')\n",
    "invite('李四')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### T32 修改模板-修改标签\n",
    "1. 下载模板文件到本地，并重命名为template3.docx\n",
    "2. 修改下载好的模板文件，把里面的&lt;name&gt;替换成其他的标签，如GUEST\n",
    "3. 上传修改好的模板文件到在线编程系统\n",
    "4. 修改下面的代码，让代码使用新的模板文件\n",
    "5. 运行代码，并查看结果，看看是否新加的标签也被替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word\n",
    "\n",
    "def invite(name):\n",
    "    file = 'template.docx'\n",
    "    doc = word.load(file)\n",
    "    word.replace(doc, '<name>', name)\n",
    "    doc.save(name + '.docx')\n",
    "\n",
    "invite('张三')\n",
    "invite('李四')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### T33 有多个参数的函数\n",
    "1. 我们要写一个函数，可以把两个数相加并打印\n",
    "2. 下面的函数运行会报错，因为在add的括号里缺少参数，请修正它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add():\n",
    "    print(a + b)\n",
    "\n",
    "add(3,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### T34 修改模板 - 替换名字和手机号码\n",
    "1. 下载模板文件到本地，并重命名为template4.docx\n",
    "2. 修改模板文件，除了名称的标签，再添加一个标签表示手机号码：PHONE\n",
    "3. 上传修改好的模板文件到在线编程系统\n",
    "4. 修改下面的代码，使它能够同时替换名称和手机号码。\n",
    "\n",
    "- 提示1：参考T33中如何给函数传递多个参数\n",
    "- 提示2：原有代码只替换了name，需要再加一行代码替换phone."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word\n",
    "\n",
    "def invite(name):\n",
    "    file = 'template.docx'\n",
    "    doc = word.load(file)\n",
    "    word.replace(doc, '<name>', name)\n",
    "    doc.save(name + '.docx')\n",
    "\n",
    "invite('张三', '18712345678')\n",
    "invite('李四', '18712345679')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
